package com.example.kai.uno.Ranking;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.kai.uno.R;

public class RankingList extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,
        ViewPager.OnPageChangeListener {

    private static final String TAG = "RankingList";


    public static final int PAGE_ONE = 0;
    public static final int PAGE_TWO = 1;
    public static final int PAGE_THREE = 2;
    public static final int PAGE_FOUR = 3;
    private TextView titleBarText;
    private RadioGroup rgTabBar;
    private RadioButton rbChannel;
    private RadioButton rbMessage;
    private RadioButton rbBetter;
    private RadioButton rbSetting;
    private ViewPager viewpager;
    private MyFragmentPagerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.rankinglist);

        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }

        mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        initViews();
    }
    private void initViews() {
        titleBarText = (TextView) findViewById(R.id.title_bar_text);
        rgTabBar = (RadioGroup) findViewById(R.id.rg_tab_bar);
        rbChannel = (RadioButton) findViewById(R.id.rb_channel);
        rbMessage = (RadioButton) findViewById(R.id.rb_message);
        rbBetter = (RadioButton) findViewById(R.id.rb_better);
        rbSetting = (RadioButton) findViewById(R.id.rb_setting);
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        rgTabBar.setOnCheckedChangeListener(this);
        viewpager.setAdapter(mAdapter);
        viewpager.setCurrentItem(0);
        viewpager.addOnPageChangeListener(this);
        rbChannel.setChecked(true);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_channel:
                viewpager.setCurrentItem(PAGE_ONE);
                titleBarText.setText("三人模式");
                break;
            case R.id.rb_message:
                viewpager.setCurrentItem(PAGE_TWO);
                titleBarText.setText("四人模式");
                break;
            case R.id.rb_better:
                viewpager.setCurrentItem(PAGE_THREE);
                titleBarText.setText("五人模式");
                break;
            case R.id.rb_setting:
                viewpager.setCurrentItem(PAGE_FOUR);
                titleBarText.setText("无尽模式");
                break;
        }
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {

        /**
         * * 原博文中注释（state的状态有三个，0表示什么都没做，1正在滑动，2滑动完毕）有误
         * 特此更正为如下：
         *
         * SCROLL_STATE_IDLE : 值为0，表示当前页已经选定。
         * SCROLL_STATE_DRAGGING: 值为1，表示当前页面正在拖动。
         * SCROLL_STATE_SETTING: 值为2，表示当前页面正在拖动中，还没有到选定状态。
         */
        Log.i(TAG, "onPageScrollStateChanged: state::" + state);
        if (state == 2) {
            int currentItemPosition = viewpager.getCurrentItem();
            Log.w(TAG, "onPageScrollStateChanged: currentItemPosition::" + currentItemPosition);
            switch (currentItemPosition) {
                case PAGE_ONE:
                    rbChannel.setChecked(true);
                    titleBarText.setText("三人模式");
                    break;
                case PAGE_TWO:
                    rbMessage.setChecked(true);
                    titleBarText.setText("四人模式");
                    break;
                case PAGE_THREE:
                    rbBetter.setChecked(true);
                    titleBarText.setText("五人模式");
                    break;
                case PAGE_FOUR:
                    rbSetting.setChecked(true);
                    titleBarText.setText("无尽模式");
                    break;
            }
        }
    }
}
